package com.jimster.plugins.video
{
	import com.jimster.plugins.video.event.JVideoEvent;
	
	import flash.display.MovieClip;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class JVideoPlayer extends MovieClip
	{
		//Variables 
		public var VWidth:Number = 100;
		public var VHeight:Number = 100;
		public var isPaused:Boolean;
		
		private var vSound:SoundTransform;
		private var vdPlayer:Video;
		private var nStream:NetStream;
		private var nConnection:NetConnection;
		
		//Constructor
		public function JVideoPlayer()
		{
		}
		
		//Public methods
		public function initializeVideo():void
		{
			this.createVideo();
		}
		
		public function stopVideo():void
		{
			this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_STOP, this, this ) );
			this.nStream.close();
			this.vdPlayer.clear();
		}
		
		public function resumeVideo():void
		{
			this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_RESUME, this, this ) );
			this.nStream.resume();
			this.isPaused = false;
		}
		
		public function pauseVideo():void
		{
			this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_PAUSE, this, this ) );
			this.nStream.pause();
			this.isPaused = true;
		}
		
		public function playVideo( $url:String, $autoPlay:Boolean=true ):void
		{
			if( $autoPlay )
			{
				this.nStream.play( $url );
				this.isPaused = false;
			}else
			{
				this.nStream.play( $url );
				this.nStream.pause();
				this.isPaused = true;
			}
			
		}
		
		//Private methods
		private function addStreamListeners( $ns:NetStream ):void
		{
			$ns.addEventListener( NetStatusEvent.NET_STATUS, this.onNetStatus );
		}
		
		private function addConnectionListeners( $nc:NetConnection ):void
		{
			$nc.addEventListener( NetStatusEvent.NET_STATUS, this.onNetStatus );
		}
		
		private function createVideo():void
		{
			this.vdPlayer = new Video( this.VWidth, this.VHeight );
			this.vdPlayer.smoothing = true;
			
			this.nConnection = new NetConnection();
			this.addConnectionListeners( this.nConnection );
			this.nConnection.connect( null );
			
			this.vSound = new SoundTransform();
			
			this.nStream = new NetStream( this.nConnection  );
			this.addStreamListeners( this.nStream );
			this.nStream.soundTransform = this.vSound;
			this.nStream.client = { onMetaData:this.onMetaData, onXMPData:this.onXMPData };
			this.nStream.bufferTime = 3;
			
			this.vdPlayer.attachNetStream( this.nStream );
			this.addChild( this.vdPlayer );
		}
		
		//Event handlers
		public function onMetaData( info:Object ):void
		{
			//trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
			this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_METADATA, this, info ) );
		}
		
		public function onXMPData( info:Object ):void
		{
			//trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
		}
		
		private function onNetStatus( $event:NetStatusEvent ):void
		{
			trace( "Video Status: " + $event.info.code );
			
			switch( $event.info.code )
			{
				case "NetConnection.Connect.Success":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.NET_CONNECTION_SUCCESS, this, this ) );
					break;
				case "NetConnection.Connect.Failed":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.NET_CONNECTION_FAILED, this, this ) );
					break;
				case "NetStream.Buffer.Full":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_BUFFER_FULL, this, this ) );
					break;
				case "NetStream.Buffer.Flush":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_BUFFER_FLUSH, this, this ) );
					break;
				case "NetStream.Buffer.Empty":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_BUFFER_EMPTY, this, this ) );
					break;
				case "NetStream.Play.Start":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_START_VIDEO, this, this ) );
					break;
				case "NetStream.Play.Stop":
					this.dispatchEvent( new JVideoEvent( JVideoEvent.ON_FINISH_VIDEO, this, this ) );
					break;
			}
		}
		
	}
}